package com.ant.storage.utils;

import java.util.Arrays;

/**
 * @author hzd
 * @version 1.0
 * @date 2023/4/12 16:23
 */
public class StringArrayUtil {

    private StringArrayUtil(){
    }

    /**
     * 数组格式字符串转 String 数组
     *
     * @param source
     * @return
     */
    public static String[] toStringArray(String source) {
        return source.substring(1,source.length()-1).split(",");
    }

    /**
     * 数组格式字符串转 int 数组
     *
     * @param source
     * @return
     */
    public static int[] toIntArray(String source) {
        return Arrays.stream(toStringArray(source)).mapToInt(Integer::parseInt).toArray();
    }

    /**
     * 数组格式字符串转 long 数组
     *
     * @param source
     * @return
     */
    public static long[] toLongArray(String source) {
        return Arrays.stream(toStringArray(source)).mapToLong(Long::parseLong).toArray();
    }

    /**
     * 数组格式字符串转 double 数组
     *
     * @param source
     * @return
     */
    public static double[] toDoubleArray(String source) {
        return Arrays.stream(toStringArray(source)).mapToDouble(Double::parseDouble).toArray();
    }

    /**
     * 数组格式字符串转 Integer 数组
     *
     * @param source
     * @return
     */
    public static Integer[] toIntegerArray(String source) {
        String[] strings = toStringArray(source);
        Integer[] integers = new Integer[strings.length];
        for (int i = 0; i < strings.length; i++) {
            integers[i] = Integer.parseInt(strings[i]);
        }
        return integers;
    }

    /**
     * int 数组装 string  12,33
     * @param integers
     * @return
     */
    public static  String toIntegerString(Integer[]  integers) {
        String str = Arrays.toString(integers);
        String val = str.substring(1,str.length() - 1);
        return val;
    }
}
